package com.example.teamwork.impl;

import com.example.teamwork.Enum.RoleStatusEnum;
import com.example.teamwork.Shrio.Exception.ServiceException;
import com.example.teamwork.converter.RoleConverter;
import com.example.teamwork.informationGet.PageVO;
import com.example.teamwork.informationGet.RoleVO;
import com.example.teamwork.mapper.PermissionMapper;
import com.example.teamwork.mapper.RoleMapper;
import com.example.teamwork.mapper.RolePermissionMapper;
import com.example.teamwork.pojo.Permission;
import com.example.teamwork.pojo.Role;
import com.example.teamwork.pojo.RolePermission;
import com.example.teamwork.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 〈角色管理〉<br>
 * 〈〉
 * @author  SongPeng
 * @date  2021/10/19 14:25
 * @version 1.0
 */

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    @Autowired
    private PermissionMapper permissionMapper;


    @Override
    public List<Long> findMenuIdsByRoleId(Long roleId) {
        Role role = roleMapper.selectByPrimaryKey(roleId);
        if(role==null){
            throw new ServiceException("该角色已不存在");
        }
        List<Long> ids=new ArrayList<>();
        List<RolePermission> rolePermissions = rolePermissionMapper.selectByRoleId(role.getRoleId());
        if(!CollectionUtils.isEmpty(rolePermissions)){
            for (RolePermission rolePermission : rolePermissions) {
                ids.add(rolePermission.getPermissionId());
            }
        }
        return ids;
    }

    @Transactional
    @Override
    public void authority(Long roleId, Long[] mids) {
        Role role = roleMapper.selectByPrimaryKey(roleId);
        if(role==null){
            throw new ServiceException("该角色不存在");
        }
        //先删除原来的权限
        rolePermissionMapper.deleteByRoleId(role.getRoleId());
        //增加现在分配的角色
        if(mids.length>0){
            for (Long mid : mids) {
                Permission permission = permissionMapper.selectByPrimaryKey(mid);
                if(permission==null){
                    throw new ServiceException("permissionId="+mid+",菜单权限不存在");
                }else {
                    RolePermission rolePermission = new RolePermission();
                    rolePermission.setRoleId(roleId);
                    rolePermission.setPermissionId(mid);
                    rolePermissionMapper.insertSelective(rolePermission);
                }
            }
        }
    }
    /**
     * 角色列表
     * */


    @Override
    public PageVO<RoleVO> findRoleList(Integer pageNum, Integer pageSize, RoleVO roleVO) {
        PageHelper.startPage(pageNum,pageSize);
        Example o = new Example(Role.class);
        String roleName = roleVO.getRoleName();
        //模糊查询
        if (roleName!=null&&!"".equals(roleName)){
            o.createCriteria().andLike("roleName","%"+roleName+"%");
        }
        List<Role> roles = roleMapper.selectByExample(o);
        List<RoleVO> roleVOS=RoleConverter.converterToRoleVOList(roles);
        PageInfo<Role> info=new PageInfo<>(roles);
        return new PageVO<>(info.getTotal(),roleVOS);
    }

    @Override
    public void add(RoleVO roleVO) {
        String roleName=roleVO.getRoleName();
        Example o = new Example(Role.class);
        o.createCriteria().andEqualTo("roleName",roleName);
        int i = roleMapper.selectCountByExample(o);
        if(i!=0){
            throw new ServiceException("该角色名已被占用");
        }
        Role role = new Role();
        BeanUtils.copyProperties(roleVO,role);
        role.setCreateTime(new Date());
        role.setModifiedTime(new Date());
        role.setRoleStatus(RoleStatusEnum.AVAILABLE.getStatusCode());//默认启用添加的角色
        roleMapper.insert(role);

    }

    @Override
    public void deleteById(Long roleId) {
        Role role = roleMapper.selectByPrimaryKey(roleId);
        if(role==null){
            throw new ServiceException("要删除的角色不存在");
        }
        roleMapper.deleteByPrimaryKey(roleId);
        //删除对应的[角色-菜单]记录
        rolePermissionMapper.deleteByRoleId(roleId);

    }

    @Override
    public RoleVO edit(Long roleId) {
        Role role = roleMapper.selectByPrimaryKey(roleId);
        if(role==null){
            throw new ServiceException("编辑的角色不存在");
        }
        RoleVO roleVO = new RoleVO();
        BeanUtils.copyProperties(role,roleVO);
        return roleVO;
    }

    @Override
    public void update(Long roleId, RoleVO roleVO) {
        String roleName = roleVO.getRoleName();
        Role dbRole = roleMapper.selectByPrimaryKey(roleId);
        if(dbRole==null){
            throw new ServiceException("要更新的角色不存在");
        }
        Example o = new Example(Role.class);
        o.createCriteria().andEqualTo("roleName",roleName);
        List<Role> roles = roleMapper.selectByExample(o);
        if(!CollectionUtils.isEmpty(roles)){
            Role role = roles.get(0);
            if(!role.getRoleId().equals(roleId)){
                throw new ServiceException("该角色名已被占用");
            }
        }
        Role role = new Role();
        BeanUtils.copyProperties(roleVO,role);
        role.setModifiedTime(new Date());
        roleMapper.updateByPrimaryKeySelective(role);
    }


    @Override
    public void updateStatus(Long roleId, Boolean status) {
        Role role = roleMapper.selectByPrimaryKey(roleId);
        if(role==null){
            throw new ServiceException("角色不存在");
        }
        Role t = new Role();
        t.setRoleId(roleId);
        t.setRoleStatus(status?RoleStatusEnum.DISABLE.getStatusCode():
                RoleStatusEnum.AVAILABLE.getStatusCode());
        roleMapper.updateByPrimaryKeySelective(t);

    }
    @Override
    public List<Role> findAll() {
        return roleMapper.selectAll();
    }
}


